package damngoodcoffee.smart;

import java.util.List;

import com.google.inject.Inject;
import com.thoughtworks.selenium.CommandProcessor;

import damngoodcoffee.smart.commands.Command;
import damngoodcoffee.smart.commands.CommandException;

public class TestImpl implements Test {

    private Parser parser;
    private CommandProcessor commandProcessor;

    @Inject
    public TestImpl(Parser parser, CommandProcessor commandProcessor) {
	super();
	this.parser = parser;
	this.commandProcessor = commandProcessor;
    }

    public void execute(String source) throws TestException {
	try {
	    commandProcessor.start();
	    List<Command> commands = parser.parse(source);
	    for (Command command : commands) {
		command.execute();
	    }
	} catch (CommandException e) {
	    throw new TestException(e);
	} catch (ParserException e) {
	    throw new TestException(e);
	} finally {
	    commandProcessor.stop();
	}
    }

}
